//
//  SearchViewController.m
//  We365
//
//  Created by Bui Thien An on 12/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchViewController.h"
#import "ListViewController.h"

@implementation SearchViewController
@synthesize searchString;
@synthesize category;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithCategory:(Category *)c
{
    self = [super init];
    [self setCategory:[c retain]];
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIColor *bg = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"BGSearch.png"]];
    self.view.backgroundColor = bg;
    [bg release];
    
    searchString.frame = CGRectMake(searchString.frame.origin.x, searchString.frame.origin.y, searchString.frame.size.width, 35);
}

- (void)viewDidUnload
{
    [self setSearchString:nil];
    [self setCategory:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSString *)title
{
    return @"Search";
}

- (void)dealloc {
    [searchString release];
    [category release];
    [super dealloc];
}

- (IBAction)search:(id)sender 
{
    NSString *key = [searchString.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if([key length] != 0)
    {
        ListViewController *listViewController = [[ListViewController alloc] initWithCategory:category andKey:key];   
        [self.navigationController pushViewController:listViewController animated:YES];
        [listViewController release];
    }
}
- (IBAction)textFieldDoneEditing:(id)sender
{
    [sender resignFirstResponder];
}

@end
